﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using CashOrCard.Migrations;
using CashOrCard.Models;

namespace CashOrCard
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            ModelBinders.Binders.DefaultBinder = new TupleModelBinder();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            var configuration = new Configuration();
            configuration.AutomaticMigrationsEnabled = true;
            configuration.AutomaticMigrationDataLossAllowed = true;
            var migrator = new DbMigrator(configuration);
            migrator.Update();
        }

        //Custom Model Binder
        public class TupleModelBinder : DefaultModelBinder
        {
            protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
            {
                if (modelType == typeof(Tuple<Login, Register>))
                    return new Tuple<Login, Register>(new Login(), new Register());              
                return base.CreateModel(controllerContext, bindingContext, modelType);
            }
        }

    }
}